package org.example.setup.controller;

import org.example.config.utils.SmsUtils;
import org.example.model.common.Result;
import org.example.model.domain.express.ExpressList;
import org.example.setup.service.ExpressListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Random;
/**
 * 账户安全控制器，处理与账户安全相关的请求。
 */
@RestController
@RequestMapping("SetUp/AccountSafe")
public class AccountSafeController {

    @Autowired
    private ExpressListService expressListService;

    SmsUtils smsUtils = new SmsUtils();

    /**
     * 修修改用户信息
     * @param expressList
     * @return
     */
    @RequestMapping("/userUpdae")
    public Result userupdae(@RequestBody ExpressList expressList){
        int expressList1 = expressListService.userPhoneUpdate(expressList);
        if(expressList1==0){
            return Result.build(500,"修改失败");
        }else{
            return Result.build(200,"修改成功");
        }

    }

    /**
     * 修改用户绑定手机号
     * @param expressList
     * @return
     */
    @RequestMapping("/userPhoneUpdate")
    public Result userPhoneUpdate(@RequestBody ExpressList expressList){
        int expressList1 = expressListService.userPhoneUpdate(expressList);
        if(expressList1==0){
            return Result.build(500,"修改失败");
        }else{
            return Result.build(200,"修改成功");
        }

    }

    /**
     * 修改用户登录密码
     * @param expressList
     * @return
     */
    @RequestMapping("/userPasswordUpdate")
    public Result userPasswordUpdate(@RequestBody ExpressList expressList){
        int expressList1 = expressListService.userPasswordUpdate(expressList);
        if(expressList1==0){
            return Result.build(500,"修改失败");
        }else{
            return Result.build(200,"修改成功");
        }

    }

    /**
     * 修改用户支付密码
     * @param expressList
     * @return
     */
    @RequestMapping("/userPayPasswordUpdate")
    public Result userPayPasswordUpdate(@RequestBody ExpressList expressList){
        int expressList1 = expressListService.userPayPasswordUpdate(expressList);
        if(expressList1==0){
            return Result.build(500,"修改失败");
        }else{
            return Result.build(200,"修改成功");
        }

    }

    /**
     * 注销账号
     * @param expressList
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(@RequestBody ExpressList expressList){
        int expressList1 = expressListService.Delete(expressList);
        if(expressList1==0){
            return Result.build(500,"修改失败");
        }else{
            return Result.build(200,"修改成功");
        }

    }

    /**
     * 向用户发送六位数验证码
     * @param phone
     * @return
     */
    @GetMapping("/yzm/{phone}")
    public Result yzm(@PathVariable("phone") String phone){
        Random random = new Random();
        int i = 100000 + random.nextInt(900000);
        try {
            smsUtils.sendSms(phone, String.valueOf(i));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return Result.build(200,"发送成功",i);
    }

    /**
     * 根据手机号进行判断手机号是否已经绑定请他账号
     * @param phone
     * @return
     */
    @GetMapping("/phoneRepeat/{phone}")
    public Boolean phoneYz(@PathVariable("phone") String phone){
        return expressListService.phoneRepeat(phone);
    }

    @GetMapping("/qjm/{phone}")
    public Result qjm(@PathVariable("phone") String phone){
        Random random = new Random();
        int i = 100000 + random.nextInt(900000);
        try {
            smsUtils.sendSms(phone, String.valueOf(i));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return Result.build(200,"发送成功",i);
    }


}
